<?
class Email
	{
		var $nMailbox;
		var $cMailbox;
		var $aFolders = array();
		var $aFields = array();
		var $nSize = 0;
		var $nqMessages = 0;
		var $nqRecent = 0;
		var $cDriver = '';
		var $nRow = 0;

		function getBody()
		{
			$oStructure =  imap_fetchstructure ( $this->nMailbox , $this->nRow  );
			$aMimeType = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
			$cTipo = $aMimeType[$oStructure->type];
			$cSubtipo = $oStructure->ifsubtype ? $oStructure->subtype : '';
			$cDescripcion = $oStructure->ifdescription ? $oStructure->description : '';
			$cDisposicion = $oStructure->ifdisposition ? $oStructure->disposition : '';
			$cId = $oStructure->ifid ? $oStructure->id : '';
			$nTamano = $oStructure->bytes;
			$nLineas = $oStructure->lines;
			$aParametros = array();
			if ( $oStructure->ifparameters )
			{
				//foreach( $oStructure->parameters as $aParameter )  $aParametros[ $aParameter['attribute'] ] = $aParameter['value'];
			}
			$aDParametros = array();
			if ( $oStructure->ifdparameters ) foreach( $oStructure->dparameters as $aParameter )	$aDParametros[ $aParameter['attribute'] ] = $aParameter['value'];
			switch ( $oStructure->type )
			{
				case TYPETEXT : // 0	text
				case TYPEMULTIPART: // 1	multipart
				case TYPEMESSAGE: //  2	message
				case TYPEAPPLICATION: //  3	application
				case TYPEAUDIO: //  4	audio
				case TYPEIMAGE: //  5	image
				case TYPEVIDEO: //  6	video
				case TYPEOTHER: //  7	other
			}
			$cText = imap_body ( $this->nMailbox , $this->nRow , FT_UID );
			return $cText;
			//echo imap_fetchbody( $this->nMailbox , $this->nRow , '2' , FT_UID );
			/*
				if ( $nParte !=  0 )
				{
					return imap_body ( $this->nMailbox , $this->nRow , FT_UID );
				} else
				{
				}
				switch ( $oStructure->type )
				{
					case TYPETEXT : // 0	text
					case TYPEMULTIPART: // 1	multipart
					case TYPEMESSAGE: //  2	message
					case TYPEAPPLICATION: //  3	application
					case TYPEAUDIO: //  4	audio
					case TYPEIMAGE: //  5	image
					case TYPEVIDEO: //  6	video
					case TYPEOTHER: //  7	other
				}
			*/
		}
		function setPaging ( $nFrom, $nLength )
		{
			$this->nRow = $nFrom;
			$nqMaxMessages = ( $nFrom + $nLength );
			//if ( $nqMaxMessages < $this->nqMessages || $this->nqMessages == 0 ) $this->nqMessages = $nqMaxMessages;
		}
		function getMatrix()
			{
			}
		function close ()
			{
				imap_expunge ($this->nMailbox );
				imap_close ($this->nMailbox );
			}
		function __goTo( $nRow = 0)
			{
				$this->nRow = $nRow ;
			}
		function markDelete()
		{
			$bRet = imap_setflag_full( $this->nMailbox , imap_uid ( $this->nMailbox , $this->nRow  ) , "\\Deleted \\Flagged", ST_UID);
		}
		function markRead()
		{
			$bRet = imap_clearflag_full( $this->nMailbox , imap_uid ( $this->nMailbox , $this->nRow  ) , "\\Recent", ST_UID);
			$bRet = imap_setflag_full( $this->nMailbox , imap_uid ( $this->nMailbox , $this->nRow  ) , "\\Seen \\Flagged", ST_UID);
		}
		function decode( $nCode, $cText )
		{
			switch ( $nCode )
			{
				case ENC7BIT: 	// 0 7BIT
					return imap_utf7_decode( $cText );
					break;
				case ENC8BIT: 	// 1	8BIT
					return imap_8bit( $cText );
					break;
				case ENCBINARY:	// 2	BINARY
					return imap_binary( $cText );
					break;
				case ENCBASE64: // 3	BASE64
					return  imap_base64( $cText );
					break;
				case ENCQUOTEDPRINTABLE: // 4	QUOTED-PRINTABLE
					return imap_qprint($cText);
					break;
				case ENCOTHER: // 5	OTHER
					return $cText;
					break;
			}
		}
		function moveTo( $nRow )
		{
			if ( $nRow > $this->nqMessages || $nRow <= 0 )  return false;
			$aMessage = imap_fetch_overview ( $this->nMailbox , imap_uid ( $this->nMailbox , $nRow  ), FT_UID );
			$this->nRow = $nRow;
			foreach( $aMessage  as $oMessage )
			{
				// echo $oMessage->message_id . $oMessage->uid . $oMessage->msgno ;
				$this->aFields['bLeido'] = ($oMessage->seen == 1 )  ? true :false;
				$this->aFields['dEnvio'] = gmdate( "Y-m-d H:i", strtotime( $oMessage->date ) ) ;
				$this->aFields['nTamano'] = $oMessage->size;
				$this->aFields['ncPrioridad'] = 1;
				$this->aFields['ncMensaje'] = $this->nRow ;
				$this->aFields['Emisor']  = $oMessage->from;
				$cdMensaje = utf8_decode( imap_utf8(  $oMessage->subject ) );
				$this->aFields['cdMensaje'] = $cdMensaje;
			}
			return true;
		}
		function moveNext()
			{
				if ( $this->nRow >= $this->nqMessages )  return false;
				return $this->moveTo( ++$this->nRow);
			}
		function count()
			{
				return $this->nqMessages;
			}
		function open( $cMailbox, $cUser, $cPassword )
			{
				$this->cMailbox = $cMailbox;
				$this->nMailbox = imap_open( $cMailbox, $cUser, $cPassword );
				if ( $this->nMailbox  == FALSE )
				{
					$cError = imap_last_error() ;
					if ( !imap_ping ( $this->nMailbox) )  $cError .= '. No puede conectarse con el servidor' ;
					echo $cError;
					addMessage( $cError);
					return false;
				}
				$oCheck = imap_check ( $this->nMailbox );
				$this->aFolders = imap_list( $this->nMailbox,  $cMailbox, "*");
				$this->cDriver = $oCheck->Driver; //$oCheck->Mailbox ;
				$this->nSize = $oCheck->Size;
				$this->nqMessages = $oCheck->Nmsgs;
				$this->nqRecent = $oCheck->Recent;
			}
		function Email( $cMailbox, $cUser, $cPassword )
			{
				$this->open( $cMailbox, $cUser, $cPassword  );
			}
	}
?>
